from flask_admin.contrib.sqla import ModelView
from flask_admin.contrib import rediscli
from flask_admin.contrib.fileadmin import FileAdmin

from redis import Redis
from app.settings.constants import BASE_URL_PREFIX


from flask_admin import Admin
from app.models.user import User, PermissionPath, Role, PermissionPathSon
from app.models.collection import (
    DataCollection,
    CollectionImage,
    ParkingCollection,
    ParkingCollectionImage,
)
from app.models.parking_rate import ParkingRate, ParkingImage
from app.models.car import CarInfo
from app.models.road_toll import RoadToll, RoadTollImage
from app.models.ruel_cost import RuelCost, RuelCostImage

from app.models import db


class RuelCostImageAdmin(ModelView):
    column_labels = {"id": "id", "file": "地址", "ruel_cost": "燃油数据"}


class ParkingImageAdmin(ModelView):
    column_labels = {"id": "id", "file": "地址", "parking": "停车场"}


class RoadTollImageAdmin(ModelView):
    column_labels = {"id": "id", "file": "地址", "road_toll": "过路费"}


class CollectionImageAdmin(ModelView):
    column_labels = {"id": "id", "file": "地址", "collection": "数据采集"}


class ParkingCollectionImageAdmin(ModelView):
    column_labels = {"id": "id", "file": "地址", "parking_collection": "数据采集"}


class PermissionPathSonAdmin(ModelView):
    column_labels = {"id": "id", "path": "路径", "path_name": "路径名称", "icon": "icon", "parent": "父级"}


class ParkingRateAdmin(ModelView):
    column_labels = {
        "id": "id",
        "parking": "停车场名称",
        "price": "价格",
        "engineer": "工程师",
        "remarks": "备注",
        "create_time": "创建时间",
        "enter_time": "进入时间",
        "out_time": "出时间",
        "user": "关联用户",
        "car": "关联用户",
        "deleted": "是否删除",
    }


class RoadTollAdmin(ModelView):
    column_labels = {
        "id": "id",
        "enterance": "入口",
        "export": "出口",
        "price": "价格",
        "engineer": "工程师",
        "remarks": "备注",
        "user": "关联用户",
        "car": "关联车辆",
        "deleted": "是否删除",
    }


class RuelCostAdmin(ModelView):
    column_labels = {
        "id": "id",
        "mileage": "里程",
        "oil_quantity": "加油量",
        "price": "价格",
        "total_price": "总价",
        "engineer": "工程师",
        "remarks": "备注",
        "create_time": "加油时间",
        "user": "关联用户",
        "car": "关联车",
        "deleted": "是否删除",
    }


class CarInfoAdmin(ModelView):
    column_labels = {
        "id": "id",
        "car_num": "车号",
        "asset_code": "资产编码",
        "vin_num": "VIN码",
        "ruel_cost": "关联燃油费",
        "road_toll": "关联路桥费",
        "parking_rate": "关联停车费",
        "deleted": "是否删除",
    }


class ParkingCollectionAdmin(ModelView):
    column_labels = {
        "id": "id",
        "create_time": "采集时间",
        "update_time": "上传时间",
        "city": "采集城市",
        "collection_type": "采集类型",
        "find_park_time": "车位寻找时间",
        "find_park_end_time": "车位寻找结束时间",
        "stop_time": "泊车结束时间",
        "end_time": "采集结束时间",
        "parking_type": "停车场特征",
        "excavate": "是否挖掘",
        "place_type": "车位特征",
        "time_type": "时间特征",
        "othen_type": "其他特征",
        "collection_place": "采集地点",
        "follow_user": "采集人员",
        "park_num": "泊入车位数量",
        "upload": "是否上传",
        "remarks": "备注",
        "user": "用户",
        "cars": "车辆",
        "images": "图片",
        "deleted": "是否删除",
    }


class DataCollectionAdmin(ModelView):
    column_labels = {
        "id": "id",
        "create_time": "采集时间",
        "update_time": "上传时间",
        "area": "省份",
        "city": "城市",
        "work_type": "作业类型",
        "project_name": "工程名",
        "course": "采集里程",
        "file_size": "文件大小",
        "file_num": "文件数量",
        "work_time": "作业时长",
        "weather": "天气",
        "data_scene": "数据场景",
        "road_category": "道路类别",
        "collection_place": "采集地点",
        "follow_user": "跟车人员",
        "remarks": "备注",
        "user": "用户",
        "deleted": "是否删除",
    }


class UserAdmin(ModelView):
    column_labels = {
        "id": "用户id",
        "username": "用户名",
        "password": "用户密码",
        "role_id": "角色ID",
        "deleted": "是否删除",
    }


class RoleAdmin(ModelView):
    column_labels = {"id": "角色id", "role_name": "角色名称", "users": "用户", "path": "路由"}


class PermissionPathAdmin(ModelView):
    column_labels = {"id": "权限ID", "path": "路径", "path_name": "路径名称", "icon": "图标", "role": "角色"}


def create_model_admin(app):
    # set optional bootswatch theme
    admin = Admin(
        app, name="flask-admin", template_mode="bootstrap3", url=BASE_URL_PREFIX + "/admin"
    )

    # Add administrative views here
    admin.add_view(PermissionPathSonAdmin(PermissionPathSon, db.session))
    admin.add_view(ParkingRateAdmin(ParkingRate, db.session))
    admin.add_view(ParkingImageAdmin(ParkingImage, db.session))
    admin.add_view(CarInfoAdmin(CarInfo, db.session))
    admin.add_view(RoadTollAdmin(RoadToll, db.session))
    admin.add_view(RoadTollImageAdmin(RoadTollImage, db.session))
    admin.add_view(RuelCostAdmin(RuelCost, db.session))
    admin.add_view(RuelCostImageAdmin(RuelCostImage, db.session))
    admin.add_view(DataCollectionAdmin(DataCollection, db.session))
    admin.add_view(CollectionImageAdmin(CollectionImage, db.session))
    admin.add_view(UserAdmin(User, db.session))
    admin.add_view(RoleAdmin(Role, db.session))
    admin.add_view(PermissionPathAdmin(PermissionPath, db.session))
    admin.add_view(ParkingCollectionAdmin(ParkingCollection, db.session))
    admin.add_view(ParkingCollectionImageAdmin(ParkingCollectionImage, db.session))
    # Add redis admin
    admin.add_view(rediscli.RedisCli(Redis()))
